package cn.yedong.org.service.impl;

import cn.yedong.basic.service.Impl.BaseServiceImpl;
import cn.yedong.basic.util.BaiduAiUtils;
import cn.yedong.org.domain.Employee;
import cn.yedong.org.domain.Shop;
import cn.yedong.org.domain.ShopAuditLog;
import cn.yedong.org.mapper.EmployeeMapper;
import cn.yedong.org.mapper.ShopAuditLogMapper;
import cn.yedong.org.mapper.ShopMapper;
import cn.yedong.org.service.IShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    //Java发送邮件
    @Autowired
    private JavaMailSender javaMailSender;
    /**
     * 店铺入驻业务
     * @param shop
     */
    @Override
    public void register(Shop shop) {
        //非空判断
        if(StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getTel())){
            throw new RuntimeException("信息不能为空！");
        }
        //密码校验
        if(!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())){
            throw new RuntimeException("两次密码不一致！");
        }
        //重复注册校验
        Shop dbShop = shopMapper.findByNameAndAdress(shop);
        if(dbShop != null){
            throw new RuntimeException("店铺已经注册，请直接登陆！如果忘记密码，请联系管理员！");
        }
        //百度AI智能审核【店铺名，logo】
        if(!BaiduAiUtils.TextCensor(shop.getName())){
            throw new RuntimeException("店铺名不合规，请重新输入！");
        }
        if(!BaiduAiUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
            throw new RuntimeException("店铺logo不合规，请重新上传！");
        }
        // 保存注册信息
        //1.添加店长信息
        Employee admin = shop.getAdmin();
        employeeMapper.add(admin);//xml设置添加员工时获取自增id
        //2.添加店铺信息
        shop.setAdminId(admin.getId()); //将店长id设置到店铺的AdminId
        shopMapper.add(shop);
        //3.将店铺自增长id设置到店长的shopId
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }

    /**
     * 审核通过的业务
     * @param shopAuditLog
     */
    @Override
    public void auditPass(ShopAuditLog shopAuditLog) throws MessagingException {
        //将店铺状态设置为 2 -待激活
        shopMapper.updateState(2,shopAuditLog.getShopId());
        //添加审核日志
        shopAuditLog.setState(1);
        shopAuditLog.setAuditId(350L);
        shopAuditLogMapper.update(shopAuditLog);
        //发送激活邮件
        //创建复杂邮件对象
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
        helper.setFrom("974431480@qq.com");
        helper.setSubject("审核成功通知！");
        //根据shopId获取shop
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        helper.setText("<h1>恭喜你，店铺已注册成功!</h1><img src='http://123.207.27.208/"+shop.getLogo()+"'><a href='http://127.0.0.1:8080/shop/activate/"+shopAuditLog.getShopId()+"'>点击该链接激活</a>",true);
        //获取店长邮箱
        String shopAdminEmail = employeeMapper.loadById(shop.getAdminId()).getEmail();
        helper.setTo(shopAdminEmail);
        //发送邮件
        javaMailSender.send(message);
    }

    /**
     * 审核驳回的业务
     * @param shopAuditLog
     */
    @Override
    public void auditReject(ShopAuditLog shopAuditLog) {
        //将店铺的状态设置为 4 - 驳回
        shopMapper.updateState(4,shopAuditLog.getShopId());
        //添加审核日志
        shopAuditLog.setState(0);
        shopAuditLog.setAuditId(350L);
        shopAuditLogMapper.add(shopAuditLog);
        //发送通知邮件
        SimpleMailMessage simpleMessage = new SimpleMailMessage();
        simpleMessage.setFrom("974431480@qq.com");
        simpleMessage.setSubject("店铺审核失败通知！");
        simpleMessage.setText("抱歉，您的店铺审核失败，请重新提交有效信息！");

        //查询:店铺-管理员-邮箱
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        Employee admin = employeeMapper.loadById(shop.getAdminId());
//        Employee admin = shop.getAdmin();?//这里面的getAdmin获取的是用于接收注册时的店长信息，数据库中没有该字段

        String email = admin.getEmail();

        //设置收件人
        simpleMessage.setTo(email);

        //发送邮件
        javaMailSender.send(simpleMessage);
    }

    /**
     * 店铺激活
     * @param shopId
     */
    @Override
    public void activate(Long shopId) {
        //1.修改店铺状态
        shopMapper.updateState(3,shopId);
        //2.添加审核日志
        ShopAuditLog shopAuditLog = new ShopAuditLog();
        shopAuditLog.setState(1);
        shopAuditLog.setAuditId(350L);
        shopAuditLog.setNote("店铺激活成功！");
        shopAuditLogMapper.add(shopAuditLog);
        //3.修改店长状态
        Long adminId = shopMapper.loadById(shopId).getAdminId();
        Employee employee = employeeMapper.loadById(adminId);
        employee.setState(1);
        employeeMapper.update(employee);
    }
}
